package com.lago.edu.controller;

import com.lago.edu.pojo.QueryVo;
import com.lago.edu.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.UUID;
import java.util.logging.SimpleFormatter;

/**
 * @author stevejson
 */
@Controller
@RequestMapping("/demo")
public class DemoController {

    @RequestMapping("/handle01")
    public ModelAndView handle01(@ModelAttribute("name") String name){
        System.out.println(name);
//        int a= 1/0;
        Date date = new Date();
        //封装了数据和页面的信息ModelAndView
        ModelAndView modelAndView = new ModelAndView();
        //addObject相当于向请求域中添加至request.setAttribute("date", date)
        modelAndView.addObject("date", date);
        //物理视图名
        //modelAndView.setViewName("/WEB-INF/jsp/success.jsp");
        //逻辑视图名
        modelAndView.setViewName("success");
        return modelAndView;
    }


    @RequestMapping("/handle02")
    public ModelAndView handle02(HttpServletRequest request, HttpServletResponse response, HttpSession session){
        String id = request.getParameter("id");
        System.out.println(id);
        Date date = new Date();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("date", date);
        modelAndView.setViewName("success");
        return modelAndView;
    }

    @RequestMapping("/handle03")
    public ModelAndView handle03(@RequestParam("id") Integer id, HttpServletRequest request, HttpServletResponse response, HttpSession session){
        System.out.println(id);
        Date date = new Date();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("date", date);
        modelAndView.setViewName("success");
        return modelAndView;
    }

    @RequestMapping("/handle04")
    public ModelAndView handle04(User user){
        System.out.println(user);
        Date date = new Date();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("date", date);
        modelAndView.setViewName("success");
        return modelAndView;
    }

    /**
     * http://localhost:8080/demo/handle05?user.id=1&user.username=wqe
     */
    @RequestMapping("/handle05")
    public ModelAndView handle05(QueryVo queryVo){
        System.out.println(queryVo);
        Date date = new Date();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("date", date);
        modelAndView.setViewName("success");
        return modelAndView;
    }

    @RequestMapping("/handle06")
    public ModelAndView handle06(Date birthday){
        System.out.println(birthday);
        Date date = new Date();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("date", date);
        modelAndView.setViewName("success");
        return modelAndView;
    }

    @RequestMapping("/handle11")
    public String handle11(ModelMap modelMap){
        Date date = new Date();
        modelMap.addAttribute("date", date);
        return "success";
    }


    @RequestMapping("/handle12")
    public String handle12(Model model){
        Date date = new Date();
        model.addAttribute("date", date);
        return "success";
    }

    @RequestMapping("/handle13")
    public String handle13(Map<String, Object> map){
        Date date = new Date();
        map.put("date", date);
        System.out.println(map.getClass());
        return "success";
    }



    @RequestMapping(value = "/handle15/{id}", method = {RequestMethod.GET})
    public String handle15(Map<String, Object> map, @PathVariable("id") Integer id){
        System.out.println(id);
        Date date = new Date();
        map.put("date", date);
        return "success";
    }


    @RequestMapping(value = "/handle", method = {RequestMethod.POST})
    public String handlePost(Map<String, Object> map, @RequestParam("username") String username){
        System.out.println(username);
        Date date = new Date();
        map.put("date", date);
        return "success";
    }

    @RequestMapping(value = "/handle/{id}/{username}", method = {RequestMethod.PUT})
    public String handlePut(Map<String, Object> map, @PathVariable("id") Integer id, @PathVariable("username") String username){
        System.out.println(username);
        System.out.println(id);
        Date date = new Date();
        map.put("date", date);
        return "success";
    }

    @RequestMapping(value = "/handle/{id}", method = {RequestMethod.DELETE})
    public String handleDelete(Map<String, Object> map, @PathVariable("id") Integer id){
        System.out.println(id);
        Date date = new Date();
        map.put("date", date);
        return "success";
    }


    @RequestMapping(value = "/handle07", method = {RequestMethod.POST})
    public @ResponseBody User handle07(Map<String, Object> map, @RequestBody User user){
        return user;
    }


    @RequestMapping(value = "/upload", method = RequestMethod.POST, consumes = "multipart/form-data")
    public @ResponseBody String upload(@RequestParam("file") MultipartFile file, HttpSession session) throws IOException {

        String originalFilename = file.getOriginalFilename();
        String ext = originalFilename.substring(originalFilename.indexOf(".") + 1, originalFilename.length());
        String newName = UUID.randomUUID().toString()+"."+ext;
        String realPath = session.getServletContext().getRealPath("/uploads");
        String datePath = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        File folder = new File(realPath+"/"+datePath);
        if(!folder.exists()){
            folder.mkdirs();
        }
        file.transferTo(new File(folder, newName));
        return "user";
    }

//    @ExceptionHandler(ArithmeticException.class)
//    public void handleException(ArithmeticException e, HttpServletResponse response) throws IOException {
//        response.getWriter().write(e.getMessage());
//    }


    @RequestMapping("/handleRedirect")
    public String handleRedirect(String name, RedirectAttributes redirectAttributes){
        //安全性  参数长度都有限制
//        return "redirect:handle01?name="+name;

        //该方法设置了一个flash属性，暂存到session中，跳转成功后销毁。
        redirectAttributes.addFlashAttribute("name", name);
        return "redirect:handle01";
    }


}
